﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Collections;

using Effects.GUI;

namespace Effects.Application
{
    /// <summary>
    ///  Class loads effects from directory given to the constructor.
    /// </summary>
    /// <remarks>
    /// <para>
    ///  This class loads all effect that are in builded dll-s in directory. This direcotry is specified in constructor. 
    ///  Load method goes through all files located in the directory and loads effects from them. (it uses Loader class)
    /// </para>
    /// </remarks>
    public class EffectLoader
    {
        /// <summary>
        ///  Path to the directory.
        /// </summary>
        private string path;

        /// <summary>
        ///  List of loaded effects.
        /// </summary>
        private List<Type> effects;

        /// <summary>
        ///  List of corrupted files from given directory.
        /// </summary>
        private List<string> corruptedFiles;

        /// <summary>
        ///  Creates new instance that can load effect from given directory.
        /// </summary>
        /// <param name="path"> path to the directory </param>
        public EffectLoader(string path)
        {
            this.path = path;
            this.corruptedFiles = new List<string>();
            effects = new List<Type>();
        }

        /// <summary>
        ///  Loads effects.
        /// </summary>
        public void Load()
        {
            string[] files = Directory.GetFiles(path);
            foreach (string file in files)
            {
                EffectFromDLLLoader load = new EffectFromDLLLoader(file);
                load.Load();
                if (load.FileOk())
                {
                    Type[] fxs = load.GetEffects();
                    effects.AddRange(fxs);
                }
                else
                    corruptedFiles.Add(file);
            }
        }

        /// <summary>
        ///  Access to the loaded types of effects.
        /// </summary>
        /// <returns> Array of all types that were loaded</returns>
        public Type[] GetEffects()
        {
            return effects.ToArray();
        }

        /// <summary>
        ///  Gets array containing paths to the corrupted files from directory.
        /// </summary>
        /// <returns> Paths to the corrupted files </returns>
        public string[] GetCorruptedFiles()
        {
            return corruptedFiles.ToArray();
        }
    }
}
